// -----------------------------------------------------------------------
// <copyright file="EmailAddressManager.cs" company="Imar.Spaanjaars.Com">
//   Copyright 2008 - 2009 - Imar.Spaanjaars.Com. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Spaanjaars.ContactManager.BusinessEntities;
using Spaanjaars.ContactManager.Dal;
using Spaanjaars.Validation;

namespace Spaanjaars.ContactManager.Bll
{
	/// <summary>
	/// The EmailAddressManager class is responsible for managing BusinessEntities.EmailAddress objects in the system.
	/// </summary>
	[DataObjectAttribute()]
	public static class EmailAddressManager
	{
		#region Public Methods

		/// <summary>
		/// Gets a list with EmailAddress objects for the requested contact person.
		/// </summary>
		/// <param name="contactPersonId">The ID of the ContactPerson for whom the e-mail addresses should be returned.</param>
		/// <returns>A list with e-mail address objects when the database contains e-mail addresses for the contact person, or an empty list otherwise.</returns>
		[DataObjectMethod(DataObjectMethodType.Select, true)]
		public static EmailAddressCollection GetList(int contactPersonId)
		{
			return GetList(contactPersonId, -1, -1);
		}

		/// <summary>
		/// Gets a list with EmailAddress objects for the requested contact person.
		/// </summary>
		/// <param name="contactPersonId">The ID of the ContactPerson for whom the e-mail addresses should be returned.</param>
		/// <returns>A list with e-mail address objects when the database contains e-mail addresses for the contact person, or an empty list otherwise.</returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static EmailAddressCollection GetList(int contactPersonId, int startRowIndex, int maximumRows)
		{
			return EmailAddressDB.GetList(contactPersonId, startRowIndex, maximumRows);
		}

		/// <summary>
		/// Gets the number of e-mail addresses for the requested contact person.
		/// </summary>
		/// <param name="contactPersonId">The id of the contact person.</param>
		public static int SelectCountForGetList(int contactPersonId)
		{
			return EmailAddressDB.SelectCountForGetList(contactPersonId);
		}

		/// <summary>
		/// Gets a single EmailAddress from the database.
		/// </summary>
		/// <param name="id">The unique ID of the e-mail address in the database.</param>
		/// <returns>An EmailAddress object when the Id exists in the database, or <see langword="null"/> otherwise.</returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static EmailAddress GetItem(int id)
		{
			return EmailAddressDB.GetItem(id);
		}

		/// <summary>
		/// Saves an e-mail address in the database.
		/// </summary>
		/// <param name="myEmailAddress">The EmailAddress instance to save.</param>
		/// <returns>The new ID of the PhoneNumber is new in the database or the existing ID when an item was updated.</returns>
		[DataObjectMethod(DataObjectMethodType.Update, true)]
		public static int Save(EmailAddress myEmailAddress)
		{
			if (!myEmailAddress.Validate())
			{
				throw new InvalidSaveOperationException("Can't save an invalid EmailAddress. Please make sure IsValid() returns true before you call Save.");
			}
			myEmailAddress.Id = EmailAddressDB.Save(myEmailAddress);
			return myEmailAddress.Id;
		}

		/// <summary>
		/// Deletes an e-mail address from the database.
		/// </summary>
		/// <param name="myEmailAddress">The EmailAddress instance to delete.</param>
		/// <returns>Returns <c>true</c> when the object was deleted successfully, or <c>false</c> otherwise.</returns>
		[DataObjectMethod(DataObjectMethodType.Delete, true)]
		public static bool Delete(EmailAddress myEmailAddress)
		{
			return EmailAddressDB.Delete(myEmailAddress.Id);
		}

		#endregion

		#region IComparable

		/// <summary>
		/// Implements IComparer&lt;EmailAddress&gt; to allow EmailAddress objects to be sorted.
		/// </summary>
		private class EmailAddressComparer : IComparer<EmailAddress>
		{
			private string _sortColumn;
			private bool _reverse;
			/// <summary>
			/// Constructor for the EmailAddressComparer class that expects the property of the EmailAddress class to sort on.
			/// </summary>
			/// <param name="sortExpression">Contains the property of the EmailAddress class to sort on. Append [space]desc to sort in reversed order.</param>
			public EmailAddressComparer(string sortExpression)
			{
				if (string.IsNullOrEmpty(sortExpression))
				{
					sortExpression = "Id desc";
				}
				_reverse = sortExpression.ToUpperInvariant().EndsWith(" desc", StringComparison.OrdinalIgnoreCase);
				if (_reverse)
				{
					_sortColumn = sortExpression.Substring(0, sortExpression.Length - 5);
				}
				else
				{
					_sortColumn = sortExpression;
				}
			}

			/// <summary>
			/// Compares two instances of EmailAddress.
			/// </summary>
			/// <param name="x">The left hand side EmailAddress object.</param>
			/// <param name="y">The right hand side EmailAddress object.</param>
			public int Compare(EmailAddress x, EmailAddress y)
			{
				int retVal = 0;
				switch (_sortColumn.ToUpperInvariant())
				{
					case "ID":
						retVal = x.Id.CompareTo(y.Id);
						break;
					case "EMAIL":
						retVal = string.Compare(x.Email, y.Email, StringComparison.OrdinalIgnoreCase);
						break;
					case "TYPE":
						retVal = x.Type.CompareTo(y.Type);
						break;
					case "CONTACTPERSONID":
						retVal = x.ContactPersonId.CompareTo(y.ContactPersonId);
						break;
				}
				int _reverseInt = 1;
				if ((_reverse))
				{
					_reverseInt = -1;
				}
				return (retVal * _reverseInt);
			}
		}
		#endregion
	}
}